import SendMsg from "@/api/SendMsg";
import {message} from "ant-design-vue";
import router from "@/router";
import store from "../../index";
import request from "../../../util/request";
import moment from "moment"
import i18n from "@/i18n";

const PackageHistoryReport = {
    namespaced: true,
    state: {
        // table数据
        dataSource: [],

        // 查询条件
        queryForm: {
            lotId: '',
            startValue: moment().format('YYYY-MM-DD'),
            endValue: moment().format('YYYY-MM-DD'),
        },
        lotList: [],
        loadTable: false,
        dataPagination: {
            size:'small',
            current: 1,
            pageSize:14,
            total: 0,
            showTotal: (total, range) => {
                return i18n.t('menuItem.total')+` ${total} `+i18n.t('menuItem.term')
            },
        },
        // table选中行
        selectRow: {},
    },
    mutations: {
        setDataPagination(state, val) {
            state.dataPagination = val;
        },
        //查询table
        queryTableList(state) {
            // if(!state.queryForm.startValue || !state.queryForm.endValue){
            //     message.warn("Please select a start and end time! ")
            //     return
            // }
            let codeInTrx = {
                trxId: "packageHistoryReport",
                actionFlg: "Q",
                lotId: state.queryForm.lotId,
                packJobInTime: state.queryForm.startValue,
                packJobOutTime: state.queryForm.endValue,
            };
            state.dataSource = []
            state.loadTable = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.dataSource = outTrx.oaryA.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                }
            }).finally(() => {
                state.loadTable = false
            });
        },


        // 查询lot下拉框数据
        queryLotList(state,data) {
            let intrx = {
                trxId: "retLot",
                actionFlg: "QL",
                flg: "Y",
                lotId: data,
            };
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.lotList = outTrx.oaryB
                }
            })
        },

        //导出
        exportExcel(state) {
            if(!state.dataSource.length>0){
                message.warn("No Data!")
                return
            }
            let inTrx = {
                trxId: "packageHistoryReport",
                actionFlg: "E",
                lotId: state.queryForm.lotId,
            };
            let name = 'Package History Report';
            inTrx.fabIdFk = window.localStorage.getItem("fabIdMes");
            inTrx.evtUsr = window.localStorage.getItem("userIdMes");
            request({
                url:  `/EOQC/exportPackageHistoryReport/packageHistoryReport/E`,
                method: "post",
                data: {
                    trxId: inTrx.trxId,
                    strInMsg: JSON.stringify(inTrx)
                },
                responseType: 'blob',
            }).then(({data, ...res}) => {
                if (res.headers['content-type'].indexOf("application/json") !== -1) {
                    let reader = new FileReader();
                    reader.onload = event => {
                        let content = reader.result;//内容就在这里
                        let result = JSON.parse(content)
                        if (result.rtnCode !== '000000') {
                            // that.setErrorCodeForm(result.rtnMsg);
                            // that.setErrorCodeVisible(true);
                            store.commit('ErrorCode/setErrorCodeVisible', true)
                            store.commit('ErrorCode/setErrorCodeForm', result)
                        }
                    };
                    reader.readAsText(data);
                } else {
                    const blob = new Blob([data]);
                    // const fileName = new Date().getTime()+'.xls';
                    //YYYY﹣MM﹣DD  HH︰mm︰ss`
                    const fileName = name + moment().format(` YYYY-MM-DD`) + '.xls';
                    const linkNode = document.createElement('a');

                    linkNode.download = fileName; //a标签的download属性规定下载文件的名称s
                    linkNode.style.display = 'none';
                    linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
                    document.body.appendChild(linkNode);
                    linkNode.click();  //模拟在按钮上的一次鼠标单击

                    URL.revokeObjectURL(linkNode.href); // 释放URL 对象
                    document.body.removeChild(linkNode);
                }

            })
        },

        // 清空弹窗数据
        cleanData(state) {
            state.queryForm = {
                lotId: '',
                startValue: '',
                endValue: '',
            }
            state.dataSource = []
        },

    },
    getters: {},
    action: {}
}
export default PackageHistoryReport;
